﻿// JavaScript File
/*
Google map Processing
*/


//“地图”按钮
function swift2GMap()
{
    if(MAP_TYPE != "GMAP")
    {
        gmap.clearOverlays();
        MAP_TYPE = "GMAP";
    }
}

//“IMS地图”按钮
function swift2IMS()
{
    if(MAP_TYPE != "IMSMAPONLY")
    {
        if(typeof(imsOverlay) != "undefined")
        {
            gmap.clearOverlays();

            //地图地理范围设置
            var mapSize = gmap.getBounds();
            
            // 设置叠加层的大小，这里设置为地图DIV的4倍大小，也就是4个方向均延升1倍
            //var bounds = map.getBounds();
            var southWest = mapSize.getSouthWest();
            var northEast = mapSize.getNorthEast();
            var lngDelta = (northEast.lng() - southWest.lng());// / 2;
            var latDelta = (northEast.lat() - southWest.lat());// / 2;
            var rectBounds = new GLatLngBounds(
                new GLatLng(southWest.lat() - latDelta,
                            southWest.lng() - lngDelta),
                new GLatLng(northEast.lat() + latDelta,
                            northEast.lng() + lngDelta));

            
            gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
            if(typeof(imsOverlay) != "undefined")
            {
                gmap.addOverlay(imsOverlay);
                $("#btnGMap").val("地图");
                MAP_TYPE = "IMSMAP";
            }
            MAP_TYPE = "IMSMAPONLY";
        }
        else
        {
            alert("您当前并没有绘制任何ArcIMS地图，所以不能切换到ArcIMS地图模式！");
            return;
        }
    }
}

//“混合”按钮
function swift2Hybird()
{
    if(MAP_TYPE != "IMSMAP")
    {
        if(typeof(imsOverlay) != "undefined")
        {
            gmap.clearOverlays();
            gmap.addOverlay(imsOverlay);
            //$("#btnGMap").val("Google地图");
            MAP_TYPE = "IMSMAP";
        }  
        else
        {
            alert("您当前并没有绘制任何ArcIMS地图，所以不能切换到ArcIMS地图模式！");
            return;
        }
    }
}

//“刷新”按钮
function RefreshMap()
{
    if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
    {
        gmap.clearOverlays();

        var tempData=$("#forData").data("dt");
        if(tempData!=undefined)
        {
           if(tempData=="disease")
           {
              getIMSMap_Disease(MAP_TYPE);
           }
           else if(tempData=="drought")  
           {
               getIMSMap(MAP_TYPE);
           }
           else
           {
              getIMSMap(MAP_TYPE);
           }
           
        }
        else
        {getIMSMap(MAP_TYPE);}

    }
    else
    {
        alert("本按钮仅供刷新ArcIMS地图，请先生成相应的专题分级图！");
        return;
    }

}


/*
Add Google Map at start.
*/
function GLoadMap(strLocation) {
    //resizePage();

    if (GBrowserIsCompatible()) 
    {
        var i = 0;
        
        gmap = new GMap2(document.getElementById("map"));
        if (strLocation.length>0)
        {
            var lat = strLocation.split(",")[0];
            var lnt = strLocation.split(",")[1];
            gmap.setCenter(new GLatLng(parseFloat(lat),parseFloat(lnt)), 9);
        }
        else
        {
//            gmap.setCenter(new GLatLng(39.913950, 116.405640), 9);
//            gmap.setCenter(new GLatLng(44.35549, 86.08187), 5);
            gmap.setCenter(new GLatLng(38.05549, 103.08187), 4);
        }
        var largeMapControl=new GLargeMapControl();
        var mapTypeControl = new GMapTypeControl();
                    
        gmap.addControl(largeMapControl);//, topLeft);
        gmap.addControl(new GScaleControl());
//        gmap.addControl(mapTypeControl);       
        
        GEvent.addListener(gmap,"zoomend",function(oldLevel, newLevel){
            
            if(MAP_TYPE == "GMAP" || MAP_TYPE == "GIMAGE") return;
            
            gmap.clearOverlays();
            
            if(MAP_TYPE != "IMSMAPONLY")
                MAP_TYPE = "IMSMAP";
          
            var tempData=$("#forData").data("dt");
		    if(tempData!=undefined)
		    {
			    if(tempData=="disease")
			    {
			        getIMSMap_Disease(MAP_TYPE);
			    }else if(tempData=="drought")
                {
                    getIMSMap(MAP_TYPE); 
                }else
			    {
			        getIMSMap(MAP_TYPE);
			    }
		    }else
		    {
			    getIMSMap(MAP_TYPE);
		    }
        });

//        当拖动地图时，刷新IMS地图
//******************************************************
//        GEvent.addListener(gmap,"dragend",function(){
//            //alert(MAP_TYPE);
//            if(MAP_TYPE != "IMSMAP") return;
//            gmap.clearOverlays();
//            getIMSMap(MAP_TYPE);
//        });
        
        
//          鼠标右键单击事件，添加标记点
//****************************************************************************        
//        GEvent.addListener(gmap,"singlerightclick",function(pixel,tile, marker) {   
//            // store the "pixel" info in case we need it later   
//            // adjust the context menu location if near an egde   
//            // create a GControlPosition   
//            // apply it to the context menu, and make the context menu visible 
//            if(rightClickResponse == true){  
//                clickedPixel = pixel;   
//                var x=pixel.x;   
//                var y=pixel.y;   
//                if (x > gmap.getSize().width - 120) { x = gmap.getSize().width - 120 }   
//                if (y > gmap.getSize().height - 100) { y = gmap.getSize().height - 100 }   
//                var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));     
//                pos.apply(contextmenu);  
//                
//                contextmenu.style.visibility = "visible"; 
//                $("#map").append(contextmenu); 
//                addPoint = false;
//                rightClickResponse = false; 
//            }
//        });   
     MAP_TYPE = "GMAP";
     
     
  }
}

function createContextMenu()
{
    var contextmenu = document.createElement("div");
    var menu1 = document.createElement("a");
    menu1.href = "#";
    var menu1Text = document.createTextNode("End");
    menu1.appendChild(menu1Text);
    contextmenu.appendChild(menu1);
    return contextmenu;
}


/*
**通过point信息对地图上面进行标注，显示相应点上的标注信息
*/
function createMarker(point, map, myHtml) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {

    marker.openInfoWindowHtml(myHtml);
   
  });
  map.addOverlay(marker);
 
}


/*
** 依据地点信息文件cities.xml，对地图进行标注
*/
function markCity(map,point){
   
	    var myHtml;
        myHtml = "<a href = '#'>" + "Hello" + "</a><br/>";
        createMarker(point, map, myHtml);        

}

//查询像元植被指数按钮
 function switchImg(){
    if(click_count)
    {document.getElementById("mpic").src="images/identify_2.gif"; click_count--;showImageInfo();}
    else
    {document.getElementById("mpic").src="images/identify_1.gif"; click_count++;removeListener();}
 }
 
//处理IMS地图服务的请求
function getIMSMap(event)
{
    var imageId = $("#ImageListSelect").val();
    document.getElementById("mdiv").style.visibility="visible";
    var graduations = "";
    var labels = "";
    var colors = "";
    var graduationNumber = $("#GraduationNumber").val();
    if (graduationNumber > 0)
    {
        if($("#defGraduations").attr("checked") == true)  
        {  
            graduations = "system," + graduationNumber;
        }
        else
        {      
            var lastValue = 0;
            for (var i = 0; i < graduationNumber; i++)
            {
                var graValue = eval($("#Graduation" + i)).val();

                if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
                if(!isDigit(graValue)) {alert("您填写的分级刻度包含非数值字符，请重新填写！");return;}
                if(graValue < lastValue) {alert("您填写的分级刻度值小于零或者小于上一级刻度值，请重新检查！");return;}
                graduations += graValue + ",";
                lastValue = parseFloat(graValue);
                var graLabel = eval($("#Label" + i)).val();
                labels += graLabel + ",";
                
                var graColor = eval($("#Color" + i)).val();
                colors += graColor + ",";
            }
        }
        if($("#defLabels").attr("checked") == true) 
        {
            labels = "system";
        }
//        else
//        {
//            for (var i = 0; i < graduationNumber; i++)
//            {
//                var graLabel = eval($("#Label" + i)).val();
//                labels += graLabel + ",";
//            }
//        }
        if($("#defColors").attr("checked") == true) 
        {
            colors = "system";
        }
//        else
//        {
//            for (var i = 0; i < graduationNumber; i++)
//            {
//                var graColor = eval($("#Color" + i)).val();
//                colors += graColor + ",";
//            }
//        }
    }
    else
    {
        alert("请设置并填写分级刻度！");
        return;
    }

    
    MAP_TYPE = "IMSMAP";//默认情况下设置MAP_TYPE为请求IMS地图服务
    if (event.data == null)
    {
        MAP_TYPE = event;
    }
    else
    {
        MAP_TYPE = event.data.flag;
    }
    
    //图片大小设置
    var divWidth = $("#map").width() * 2; 
    var divHeight = $("#map").height() * 2;
    if(MAP_TYPE == "IMSLEGEND") divWidth -= 258;
    
    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));     
    
    var mapEast = parseInt(10000*rectBounds.getNorthEast().lng())/10000;
    var mapNorth = parseInt(10000*rectBounds.getNorthEast().lat())/10000;
    var mapWest = parseInt(10000*rectBounds.getSouthWest().lng())/10000;
    var mapSouth = parseInt(10000*rectBounds.getSouthWest().lat())/10000;
    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
    //选择的模型方程及参数
    var selectedModelName = "";
    var selectedParamA = "";
    var selectedParamB = "";
    var selectedModel = "";
    var tempString = "";
    var tempStringArray = new Array();
    
    var index = -2;
    
    if($("#radioLine").attr("checked") == true) 
    {
        selectedModelName = "LinerModel";
        selectedModel = $("#txtLine").val();
        tempString = selectedModel.split('=')[1];
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }        
    }
    else if($("#radioExp").attr("checked") == true) 
    {
        selectedModelName = "ExpModel";
        selectedModel = $("#txtExp").val();
        tempString = selectedModel.split('=')[1];               
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }        
    }
    else if($("#radioLn").attr("checked") == true) 
    {
        selectedModelName = "LogModel";
        selectedModel = $("#txtLn").val();
        tempString = selectedModel.split('=')[1];       
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }        
    }
    else
    {
        hideWaitImage(); 
        alert("您没有选择相应的病虫害监测模型，因此不能制作地图，请您按步骤操作！");
        return;
    }
    
    
    var CheckLayerId = "";
    var CheckArray = $("input[name = 'checkLayer']");
    if(CheckArray.length == 0)
    {
         CheckLayerId = "none";
    } 
    else 
    {       
        for (var i = 0; i< CheckArray.length; i++)
        {
            if($(CheckArray[i]).attr("checked") == true)
            {
                CheckLayerId += $(CheckArray[i]).attr("id") + ",";
            }
        }
    }
    
    //对选择的植被指数与农学参数进行判断，是利用相关系数计算的结果还是直接利用已有模型
    var SelectedRadioId = "";
    
    if(useModel == "true")
    {
        SelectedRadioId = select_Argiparam.split(',',1) + "-" + select_RSindex.split(',',1);
    }
    else
    {
        var RadioArray = $("input[name = 'RelatedCoefficient']");

        for (var i = 0; i < RadioArray.length; i++)
        {
            if($(RadioArray[i]).attr("checked") == true)
            {
                SelectedRadioId = $(RadioArray[i]).attr("id");
                if (SelectedRadioId.indexOf("-") > -1)
                    break;
            }
        }
    }
    var SelectedRS=SelectedRadioId.substring(SelectedRadioId.indexOf("-")+1);

    if(typeof(SelectedRS) == "undefined" || SelectedRS.length == 0 )
    {
        hideWaitImage(); 
        alert("您没有选择相关系数合理的参数进行模型反演，因此不能制作地图，请您按步骤操作！");
        return;
    }
        
    //请求地图
    showWaitImage();

    var requestBaseUrl = "services/GetMap.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:USER_ID,
                            MAP_TYPE:MAP_TYPE,
                            MAPBOX:mapBox,
                            MAPWIDTH:divWidth,
                            MAPHEIGHT:divHeight,
                            GRADUATIONS:graduations,
                            LABELS:labels,
                            COLORS:colors,
                            MODELNAME:selectedModelName,
                            PARAMA:selectedParamA,
                            PARAMB:selectedParamB,
                            IMAGEID:imageId,
                            LAYERNAME:layerName,
                            SELECTEDRS:SelectedRS,  //NEW FOR CREATE MAP USE SELECTED RS INDEX DATA.
                            CHECKLAYERS:CheckLayerId, //DISPLAY LAYERS
                            TMP:Math.random()                           
                            },
                            function(data){
                                //alert(data.documentElement);
                                var xmlDocument = data.documentElement;
                                
                                if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
                                {
                                var nodes = xmlDocument.getElementsByTagName("URL");

                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";
                                    gmap.clearOverlays();

                                    if (MAP_TYPE == "IMSMAPONLY")
                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
                                    imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                    gmap.addOverlay(imsOverlay);
                                                            
                                    hideWaitImage(); 
                                }
                                else if(MAP_TYPE == "IMSLEGEND")
                                {                                                          
                                    displayData(xmlDocument);                                    
                                    
                                    MAP_TYPE = lastMapType;//恢复生成图例之前的地图类型；
                                    hideWaitImage(); 
                                }

                            });
                   
}



//根据地图发布的axl文档显示图层数据 
 function displayData(xmlDocument) 
 {
     //IMSLEGEND---javascript对象直接量参数
     var schema = {
        rowtag: "LAYER",
        columns:[
            {tagName:"visible"},{tagName:"type"},{tagName:"name"}]
        };
     var rschema = {
        rowtag:"RASTER_RANGE",
        columns:[
          {tagName:"lower"},{tagName:"color"},{tagName:"label"}]
        };
     var dschema = {
      rowtag: "DATASET",
      columns:[
        {tagName:"type"}]
      };
     
     var xmlEl = xmlDocument.getElementsByTagName(schema.rowtag);
     var table = document.createElement("table");
     table.style.font="12px arial,serif";
    // table.style.fontSize = "12px";
    // table.border = "1";
     
     var tbody = document.createElement("tbody");
     table.appendChild(tbody);
      
     for (i = 0; i < xmlEl.length; i++) 
     {	 	 
       var xmlrow = xmlEl[i];
       var row = document.createElement("tr");

       for (j = 0; j < schema.columns.length; j++)
       {
          //abstract attribute
          var sc = schema.columns[j].tagName;
          var celltext = xmlrow.getAttribute(sc);
         
          if(j == 0)
          {
     	     //Add checkbox in the first column 
     	     var scName = schema.columns[j+2].tagName;
             var textName = xmlrow.getAttribute(scName);
     	     
             var chkDotLayer = document.createElement("input");      		 
	         chkDotLayer.type = "checkbox";
	         chkDotLayer.name = "checkLayer";
	         //chkDotLayer.id = "check" + "-" + textName; 
	         chkDotLayer.id = textName;
	         if(celltext == "true")
	          {chkDotLayer.checked = true;}
	         else
	 	      {chkDotLayer.checked = false;}
	         
	         var td = document.createElement("td");
   		     td.appendChild(chkDotLayer);
   		     row.appendChild(td);
   		  }
         if(j == 1){		   			     
           // Insert the actual text/data from the XML document.
            if(celltext == "image")
            {
    	        var sname = schema.columns[j+1].tagName;
                var nametext = xmlrow.getAttribute(sname);
                var td = document.createElement("td");
 	 	        var nameData =document.createTextNode(nametext);
 	 	        td.appendChild(nameData);
 	 	        row.appendChild(td);
		        tbody.appendChild(row);           	 	  
            	
    	        var xmlEl3 = xmlDocument.getElementsByTagName(rschema.rowtag);
    	        for(m = 0; m < xmlEl3.length; m++)
    	        {
      	            var xmlrow3 = xmlEl3[m];
      	            var row = document.createElement("tr");
                  	
      	            for(n = 0; n < rschema.columns.length; n++)
      	            {
        	            var sc3 = rschema.columns[n].tagName;
 	  			        var celltext3 = xmlrow3.getAttribute(sc3);		     	  			
             	  			
 	  			        if(n == 0){
 	  				       var chkDotLayer = document.createElement("input");      		 
                           chkDotLayer.type = "hidden";      		 					
 	  				       var td = document.createElement("td");
 	  				       td.appendChild(chkDotLayer);
 	  				       row.appendChild(td);}
     	  			       
  			            if(n == 1){
  				            var lineDiv = document.createElement("div");
				            //lineDiv.style.border="thin solid #000000";
				            //lineDiv.style.borderColor = "rgb(132,216,12)";
				            lineDiv.style.backgroundColor="rgb("+celltext3+")";
				            lineDiv.style.width = "40px";
				            lineDiv.style.height = "10px";
            					
				            var td = document.createElement("td");
				            td.appendChild(lineDiv);
				            row.appendChild(td); }
				            
  			            if(n == 2){
  		  	                var td = document.createElement("td");
 				            var xmlData =document.createTextNode(celltext3);
 				            td.appendChild(xmlData);
 				            row.appendChild(td);
 	  		              } 
 	  		        }
      	            tbody.appendChild(row);
              }
 	  	      break;
 	  	    } 
 	  	    else if(celltext == "featureclass")
 	  	    {
		        var xmlEl2 = xmlDocument.getElementsByTagName(dschema.rowtag);		        
	
		        for (k = 0; k < xmlEl2.length; k++){		        
		        var xmlrow2 = xmlEl2[k];
		        var sc2 = dschema.columns[0].tagName;
		        var celltext2 = xmlrow2.getAttribute(sc2);

		        if(celltext2 == "polygon"){
			        var lineDiv = document.createElement("div");
			        lineDiv.style.border="thin solid #000000";
			        lineDiv.style.borderColor = "black";
			        lineDiv.style.width = "40px";
			        lineDiv.style.height = "10px";
					
			        var td = document.createElement("td");
			        td.appendChild(lineDiv);
			        row.appendChild(td);
			        break; }
		        else if(celltext2 == "line"){
			        var lineDiv = document.createElement("div");
		            lineDiv.style.backgroundColor = "red";
		            lineDiv.style.width = "50px";
		            lineDiv.style.height = "1px";
				
		            var td = document.createElement("td");
			        td.appendChild(lineDiv);
			        row.appendChild(td);
			        break; 
		        }}
          }}
         if(j == 2){
            var td = document.createElement("td");
 	 	    var xmlData =document.createTextNode(celltext);
 	 	    td.appendChild(xmlData);
 	 	    row.appendChild(td); }
       }
         tbody.appendChild(row);
      }
       
       var row = document.createElement("tr");
	   var td = document.createElement("td");
	   var okButton = document.createElement("input");      		 
   	   okButton.type = "button";
   	   okButton.value = "显示图层";
   	   $(okButton).bind("click",{flag:"IMSMAP"},getIMSMap);
       td.appendChild(okButton);
	   row.appendChild(td);
	   tbody.appendChild(row);
       
       document.getElementById("legendDiv").appendChild(table);
}



//*************************灾害监测-病害监测*****************
function getIMSMap_Disease(event)
{
    var imageId = $("#ImageListSelect").val();
    document.getElementById("mdiv").style.visibility="visible";
    var graduations = "";
    var labels = "";
    var colors = "";
    var graduationNumber = $("#GraduationNumber").val();
    if (graduationNumber > 0)
    {
        if($("#defGraduations").attr("checked") == true)  
        {  
            graduations = "system," + graduationNumber;
        }
        else
        {
        
            var lastValue = 0;
            for (var i = 0; i < graduationNumber; i++)
            {
                var graValue = eval($("#Graduation" + i)).val();

                if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
                if(!isDigit(graValue)) {alert("您填写的分级刻度包含非数值字符，请重新填写！");return;}
                if(graValue < lastValue) {alert("您填写的分级刻度值小于零或者小于上一级刻度值，请重新检查！");return;}
                graduations += graValue + ",";
                lastValue = parseFloat(graValue);

                var graLabel = eval($("#Label" + i)).val();
                labels += graLabel + ",";
                
                var graColor = eval($("#Color" + i)).val();
                colors += graColor + ",";
            }
        }
        if($("#defLabels").attr("checked") == true) labels = "system";
        if($("#defColors").attr("checked") == true) colors = "system";        
    }
    else
    {
        alert("请设置并填写分级刻度！");
        return;
    }
    
    
    MAP_TYPE = "IMSMAP";//默认情况下设置MAP_TYPE为请求IMS地图服务
    if (event.data == null)
    {
        MAP_TYPE = event;
    }
    else
    {
        MAP_TYPE = event.data.flag;
    }
    
    //图片大小设置
    var divWidth = $("#map").width() * 2; 
    var divHeight = $("#map").height() * 2;
    if(MAP_TYPE == "IMSLEGEND") divWidth -= 258;
    
    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));    
    
    var mapEast = parseInt(10000*rectBounds.getNorthEast().lng())/10000;
    var mapNorth = parseInt(10000*rectBounds.getNorthEast().lat())/10000;
    var mapWest = parseInt(10000*rectBounds.getSouthWest().lng())/10000;
    var mapSouth = parseInt(10000*rectBounds.getSouthWest().lat())/10000;
    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
    //选择的模型方程及参数
    var selectedModelName = "";
    var selectedParamA = "";
    var selectedParamB = "";
    var selectedModel = "";
    var tempString = "";
    var tempStringArray = new Array();
    var index = -2;
    
    if($("#radioLine").attr("checked") == true) 
    {
        selectedModelName = "LinerModel";
        selectedModel = $("#txtLine").val();
        tempString = selectedModel.split('=')[1];
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }        
    }
    else if($("#radioExp").attr("checked") == true) 
    {
        selectedModelName = "ExpModel";
        selectedModel = $("#txtExp").val();
        tempString = selectedModel.split('=')[1];               
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }        
    }
    else if($("#radioLn").attr("checked") == true) 
    {
        selectedModelName = "LogModel";
        selectedModel = $("#txtLn").val();
        tempString = selectedModel.split('=')[1];       
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }        
    }
    else
    {
        hideWaitImage(); 
        alert("您没有选择相应的病虫害监测模型，因此不能制作地图，请您按步骤操作！");
        return;
    }
    
    var SelectedRadioId = "";
    var RadioArray = $("input[name = 'RelatedCoefficient']");

    for (var i = 0; i < RadioArray.length; i++)
    {
        if($(RadioArray[i]).attr("checked") == true)
        {
            SelectedRadioId = $(RadioArray[i]).attr("id");
            if (SelectedRadioId.indexOf("-") > -1)
                break;
        }
    }
    var SelectedRS=SelectedRadioId.substring(SelectedRadioId.indexOf("-")+1); //'_'下划线应为'-'
    if(typeof(SelectedRS) == "undefined" || SelectedRS.length == 0 )
    {
        hideWaitImage(); 
        alert("您没有选择相关系数合理的参数进行模型反演，因此不能制作地图，请您按步骤操作！");
        return;
    }   
    
    //请求地图
    showWaitImage();
    var requestBaseUrl = "services/GetMap_Disease.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:USER_ID,
                            MAP_TYPE:MAP_TYPE,
                            MAPBOX:mapBox,
                            MAPWIDTH:divWidth,
                            MAPHEIGHT:divHeight,
                            GRADUATIONS:graduations,
                            LABELS:labels,
                            COLORS:colors,
                            MODELNAME:selectedModelName,
                            PARAMA:selectedParamA,
                            PARAMB:selectedParamB,
                            IMAGEID:imageId,
                            SELECTEDRS:SelectedRS,
                            LAYERNAME:layerName,
                            TMP:Math.random()                           
                            },
                            function(data){
                                //alert(data.documentElement);
                                var xmlDocument = data.documentElement;
                                var nodes = xmlDocument.getElementsByTagName("URL");
                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";
//                                hideWaitImage();

                                if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
                                {
                                    gmap.clearOverlays();                      
                                                            
                                    if (MAP_TYPE == "IMSMAPONLY")
                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
                                    imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                    gmap.addOverlay(imsOverlay);

                                    hideWaitImage(); 
                                }
                                else if(MAP_TYPE == "IMSLEGEND")
                                {
                                    var imgLegend = document.createElement("img");
                                    imgLegend.src = strResultUrl;
                                    imgLegend.alt = "图例";
                                    $('#legendDiv').empty();
                                    $('#legendDiv').append(imgLegend);
                                    
                                    MAP_TYPE = lastMapType;//恢复生成图例之前的地图类型；
                                    hideWaitImage(); 
                                }
                                
                            });
}

//********************************灾害监测--干旱******************* 
function getIMSMap_Drought(event)
{
    var imageId = $("#ImageListSelect").val();
    document.getElementById("mdiv").style.visibility="visible";
    var graduations = "";
    var labels = "";
    var colors = "";
    var graduationNumber = $("#GraduationNumber").val();
    if (graduationNumber > 0)
    {
        if($("#defGraduations").attr("checked") == true)  
        {  
            graduations = "system," + graduationNumber;
        }
        else
        {
            var lastValue = 0;
            for (var i = 0; i < graduationNumber; i++)
            {
                var graValue = eval($("#Graduation" + i)).val();

                if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
                if(!isDigit(graValue)) {alert("您填写的分级刻度包含非数值字符，请重新填写！");return;}
                if(graValue < lastValue) {alert("您填写的分级刻度值小于零或者小于上一级刻度值，请重新检查！");return;}
                graduations += graValue + ",";
                lastValue = parseFloat(graValue);

                var graLabel = eval($("#Label" + i)).val();
                labels += graLabel + ",";
                
                var graColor = eval($("#Color" + i)).val();
                colors += graColor + ",";
            }
        }
        if($("#defLabels").attr("checked") == true) labels = "system";
        if($("#defColors").attr("checked") == true) colors = "system";
        
    }
    
    
    MAP_TYPE = "IMSMAP";//默认情况下设置MAP_TYPE为请求IMS地图服务
    if (event.data == null)
    {
        MAP_TYPE = event;
    }
    else
    {
        MAP_TYPE = event.data.flag;
    }
    
    //图片大小设置
    var divWidth = $("#map").width() * 2; //gmap.getSize().width - 6;    
    var divHeight = $("#map").height() * 2;//gmap.getSize().height - 6;  
    if(MAP_TYPE == "IMSLEGEND") divWidth -= 258;
    
    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));    
    
    
    var mapEast = parseInt(10000*rectBounds.getNorthEast().lng())/10000;
    var mapNorth = parseInt(10000*rectBounds.getNorthEast().lat())/10000;
    var mapWest = parseInt(10000*rectBounds.getSouthWest().lng())/10000;
    var mapSouth = parseInt(10000*rectBounds.getSouthWest().lat())/10000;
    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
    
    //确定选择的相关系数
    var SelectedRadioId = "";
    var RadioArray = $("input[name = 'RelatedCoefficient']");

    for (var i = 0; i < RadioArray.length; i++)
    {
        if($(RadioArray[i]).attr("checked") == true)
        {
            SelectedRadioId = $(RadioArray[i]).attr("id");
            if (SelectedRadioId.indexOf("-") > -1)
                break;
        }
    }
    var SelectedRS=SelectedRadioId.substring(SelectedRadioId.indexOf("-")+1);
    if(typeof(SelectedRS) == "undefined" || SelectedRS.length == 0 )
    {
        hideWaitImage(); 
        alert("您没有选择相关系数合理的参数进行模型反演，因此不能制作地图，请您按步骤操作！");
        return;
    }
    
    //请求地图
    showWaitImage();
    var requestBaseUrl = "services/GetMap_Drought.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:USER_ID,
                            MAP_TYPE:MAP_TYPE,
                            MAPBOX:mapBox,
                            MAPWIDTH:divWidth,
                            MAPHEIGHT:divHeight,
                            GRADUATIONS:graduations,
                            LABELS:labels,
                            COLORS:colors,
                            IMAGEID:imageId,
                            //SELECTEDRS:SelectedRS,
                            TMP:Math.random()                           
                            },
                            function(data){
                                //alert(data.documentElement);
                                var xmlDocument = data.documentElement;
                                var nodes = xmlDocument.getElementsByTagName("URL");
                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";

                                if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
                                {
                                    gmap.clearOverlays();
                      
                                                            
                                    if (MAP_TYPE == "IMSMAPONLY")
                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
                                    imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                    gmap.addOverlay(imsOverlay);

                                    hideWaitImage(); 
                                }
                                else if(MAP_TYPE == "IMSLEGEND")
                                {
                                    var imgLegend = document.createElement("img");
                                    imgLegend.src = strResultUrl;
                                    imgLegend.alt = "图例";
                                    $('#legendDiv').empty();
                                    $('#legendDiv').append(imgLegend);
                                    
                                    MAP_TYPE = lastMapType;//恢复生成图例之前的地图类型；
                                    hideWaitImage(); 
                                }
                              
                            });                           
}


function showImageInfo()
{    
    //var map1 = new GMap2(document.getElementById("map"));
    //gmap.setCenter(new GLatLng(37.4419, -122.1419), 13);
    var imageId = $("#ImageListSelect").val();
    var SelectedRadioId = "";
    var SelectedRS="";
    var RadioArray = $("input[name = 'RelatedCoefficient']");

    if(RadioArray)
    {
        for (var i = 0; i < RadioArray.length; i++)
        {
            if($(RadioArray[i]).attr("checked") == true)
            {
                SelectedRadioId = $(RadioArray[i]).attr("id");
                if (SelectedRadioId.indexOf("-") > -1)
                    break;
            }
        }
        //var SelectedRS=SelectedRadioId.substring(SelectedRadioId.indexOf("-")+1);
        SelectedRS=SelectedRadioId.split("-").toString();
        
        if(useModel == "true")
        {
            SelectedRS = select_Argiparam.split(',',1) + "," + select_RSindex.split(',',1);
        }
        else
        {
            if(typeof(SelectedRS) == "undefined" || SelectedRS.length == 0 )
            {
                hideWaitImage(); 
                alert("您没有选择相关系数合理的参数进行模型反演，因此查询地图信息，请您按步骤操作！");
                return;
            }
        }
    }

    var selectedModel = "";
    var selectedParamA = "";
    var selectedParamB = "";
    var tempString = "";
    var tempStringArray = new Array();   
    var modelType=""; 
    if($("#radioLine").attr("checked") == true) 
    {
        selectedModel = $("#txtLine").val();
        modelType="line";
    }
    if($("#radioExp").attr("checked") == true) 
    {
        selectedModel = $("#txtExp").val();
        modelType="exp";
    }
    if($("#radioLn").attr("checked") == true) 
    {
        selectedModel = $("#txtLn").val();
        modelType="log";
    }
    
    var index = -2;
    if(modelType)
    {
        tempString = selectedModel.split('=')[1];
        index = tempString.indexOf("+");
        if(index == -1)
        {
            tempStringArray = tempString.split('-');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
            selectedParamB = -selectedParamB;         
        }
        else
        {
            tempStringArray = tempString.split('+');
            selectedParamA = tempStringArray[0];
            selectedParamB = parseFloat(tempStringArray[1].split('*')[0]);
        }
                    
    }else
    {
        selectedParamA=$("#param2").val();
        selectedParamB=$("#param1").val();
    }

    //图标,正在加载的提示信息图标    
    var mIcon = new GIcon(G_DEFAULT_ICON);    
    mIcon.image="icons/progressbar_microsoft.gif";
    mIcon.iconSize=new GSize(100,9);
    markerOptions = { icon:mIcon };
    var bool="";
    var point_marker;
    var lat="";
    var lng="";

         //GEvent.addDomListener(gmap,"click",openInfoWindowHandle);
         //gMapListen一个全局的google GEventListener对象
         gMapListen=GEvent.addListener(gmap,"click",function(marker, point){
            var strURL="services/getPixel.aspx";
            if(point)
            {
                gmap.setCenter(new GLatLng(point.lat(),point.lng()),gmap.getZoom());                                    
                var _marker=new GMarker(point, markerOptions);
                //alert(event.type);//在IE中是mouseup,在FF中提示错误，未定义
                gmap.addOverlay(_marker);//鼠标点击某处后，出现正在加载的提示图标 
                //var current_x=point.                                     
                $.get(strURL,{IMAGEID:imageId,X:point.x,Y:point.y,RS:SelectedRS,TMP:Math.random()},
                      function(data){
                        var xmlDocument = data.documentElement;
                        var nodes=xmlDocument.getElementsByTagName("pixel");
                        var pixel=nodes[0].firstChild.nodeValue;   
                        var nodes_1=xmlDocument.getElementsByTagName("showTXT");  
                        var showTxt=nodes_1[0].firstChild.nodeValue;                                         
                        if(isNaN(pixel))
                        {
                            gmap.openInfoWindow(gmap.getCenter(), document.createTextNode(pixel)); //错误提示
                        }else
                        {     
                            var strHtml="";   
                            var _point=tranGLatLngToDMS(point);
                            strHtml="<table><tr><td>";
                            strHtml+="经度：</td><td>"+_point[0]+"</td></tr><tr><td>";
                            strHtml+="纬度：</td><td>"+_point[1]+"</td></tr><tr><td>";                                        
                            switch(modelType)
                            {
                                case "":
                                case "line":
                                    strHtml+=showTxt+"：</td><td>"+tofloat((Number(selectedParamA)+Number(pixel*selectedParamB)),4)+"</td></tr>";                                                        
                                    break;
                                case "exp":
                                    strHtml+=showTxt+"：</td><td>"+tofloat((Number(selectedParamA)+Number(Math.exp(pixel)*selectedParamB)),4)+"</td></tr>";                                                        
                                    break;
                                case "log":
                                    strHtml+=showTxt+"：</td><td>"+tofloat((Number(selectedParamA)+Number(Math.log(pixel)*selectedParamB)),4)+"</td></tr>";                                                        
                                    break;
                            }  
                            strHtml+="</table>";
                            gmap.openInfoWindowHtml(gmap.getCenter(),strHtml); //在给定的点打开一个信息窗口                                       
                        }                                              
                        gmap.removeOverlay(_marker);  //移除正在加载的提示图标                                                                             
                       });                                    
                if(bool)
                {
                    gmap.removeOverlay(point_marker);//移除上次点击的坐标点图标
                    bool=0;
                }
                point_marker=new GMarker(point);
                gmap.addOverlay(point_marker);//添加一个当前点击处的坐标点图标
                bool=1;  
            }else
            {
                gmap.removeOverlay(point_marker);//关闭信息窗口后，移除上次点击的坐标点图标
            }                            
         
         });
}


function removeListener()
{
    GEvent.removeListener(gMapListen);
}